package com.zte.claa.inficombo.csa.app.service.redis;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import com.zte.claa.inficombo.csa.app.model.app.CSConnState;
import com.zte.claa.inficombo.csa.app.model.app.CSQuitState;
import com.zte.claa.inficombo.csa.app.model.dev.DevJoinState;
import com.zte.claa.inficombo.csa.app.model.dev.DevRunState;
import com.zte.claa.inficombo.csa.app.model.redis.SysLog;
import com.zte.claa.inficombo.csa.app.model.redis.XStateData;
import com.zte.claa.inficombo.csa.app.util.JsonUtils;

public class PublishDataBuilder {

	// 构造日志.
	/**
	 * @return
	 */
	public static SysLog buildSysLog(String title, byte ret, String module, String func, String info){
		SysLog log = new SysLog();
		log.setLogid(genLogID());
		log.setLogtime(new Timestamp(System.currentTimeMillis()));
		log.setTitle(title);
		log.setModule(module);
		log.setRet(ret);
		log.setFunc(func);
		log.setInfo(info);		
		return log;
	}
	
	/**
	 * 产生日志的UUID
	 * 
	 * @return
	 */
	public static String genLogID(){
		SimpleDateFormat pureDTFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		StringBuffer buf = new StringBuffer("L");
		buf.append(Math.abs(UUID.randomUUID().getLeastSignificantBits()));
		buf.append("T").append(pureDTFormat.format(new Date(System.currentTimeMillis())));
		return buf.toString();
	}
	
	/**
	 * 构造devrunstate的redis发布消息（json字符串）
	 * 
	 * @param state
	 * @return
	 */
	public static String buildDevRunStateMsg(DevRunState state){
		XStateData xsData = new XStateData();
		xsData.setData(state);
		xsData.setMsgtype(XStateData.TP_DEVONLINE);
		xsData.setxEUI(state.getDevEUI());
		return JsonUtils.toJson(xsData);
	}
	
	/**
	 * 构造dev join state的redis发布消息（json字符串）
	 * 
	 * @param state
	 * @return
	 */
	public static String buildDevJoinStateMsg(DevJoinState state){
		XStateData xsData = new XStateData();
		xsData.setData(state);
		xsData.setMsgtype(XStateData.TP_DEVJOIN);
		xsData.setxEUI(state.getDevEUI());
		return JsonUtils.toJson(xsData);
	}
	
	/**
	 * 构造appeui csif连接退出的redis发布消息（json字符串）
	 * 
	 * @param state
	 * @return
	 */
	public static String buildCSQuitStateMsg(CSQuitState state){
		XStateData xsData = new XStateData();
		xsData.setData(state);
		xsData.setMsgtype(XStateData.TP_CSQUIT);
		xsData.setxEUI(state.getAppEUI());
		return JsonUtils.toJson(xsData);
	}
	
	/**
	 * 构造appeui csif连接建立的redis发布消息（json字符串）
	 * 
	 * @param state
	 * @return
	 */
	public static String buildCSConnStateMsg(CSConnState state){
		XStateData xsData = new XStateData();
		xsData.setData(state);
		xsData.setMsgtype(XStateData.TP_CSCONN);
		xsData.setxEUI(state.getAppEUI());
		return JsonUtils.toJson(xsData);
	}
	
}
